<?php

namespace Biz\module\Classes\mgt;

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\module\Classes\core\SearchListAbstract;
use Medoo\Medoo;

class RightsList extends SearchListAbstract
{
    protected function buildSelect()
    {
        $this->select = '*';
    }

    protected function buildWhere()
    {
        $this->where['shop_id'] = $this->params['shop_id'];

        if (!empty($this->params['status'])) {
            $this->where['rights_status'] = $this->params['status'];
        } else {
            $this->where['rights_status[!]'] = 0;
        }

        if (!empty($this->params['order_no'])) {
            $this->where['order_no'] = htmlspecialchars($this->params['order_no']);
        }

        if (!empty($this->params['refund_order_no'])) {
            $this->where['refund_order_no'] = htmlspecialchars($this->params['refund_order_no']);
        }

        if (!empty($this->params['start_rights_time'])) {
            $this->where['launch_rights_time[>=]'] = date('Y-m-d H:i:s', strtotime($this->params['start_rights_time']));
        }
        if (!empty($this->params['stop_rights_time'])) {
            $this->where['launch_rights_time[<]'] = date('Y-m-d H:i:s', strtotime($this->params['stop_rights_time']));
        }
    }

    public function getCount()
    {
        return mOrderRefund::count($this->where);
    }

    public function getList()
    {
        return mOrderRefund::select($this->select, array_merge($this->where, ['LIMIT' => $this->limit, 'ORDER' => ['addtime' => 'DESC']]));;
    }

    public function rightsList()
    {
        $data = [];

        $data['count'] = $this->getCount();

        $data['list'] = $this->getList();

        $data['all_count'] = $this->allCount();

        $data['all_count'][0] = mOrderRefund::count(['shop_id' => $this->params['shop_id'], 'rights_status[!]' => 0]); //全部状态

        array_walk($data['list'], function (&$v, $k) use (&$data) {
            mOrderRefund::formatInfo($v,'mgt');

            $data['list'][$k]['sku_info'] = [
                'sku_id'    => $v['sku_id'],
                'goods_id'  => $v['goods_id'],
                'title'     => $v['title'],
                'sku_desc'  => $v['sku_desc'],
                'cover_img' => $v['cover_img_thumb_url']
            ];
        });

        return $data;
    }

    protected function allCount()
    {
        $counts = [
            mOrderRefund::RIGHTS_STATUS_PROOFING => 0, //待处理（举证中）
            mOrderRefund::RIGHTS_STATUS_NORMAL   => 0, //待平台介入
            mOrderRefund::RIGHTS_STATUS_JUDGE    => 0 //维权完成
        ];
        $allCount = mOrderRefund::select(['counts' => Medoo::raw('COUNT(<refund_order_no>)'), 'rights_status'], ['GROUP' => 'rights_status', 'shop_id' => $this->params['shop_id']]);
        array_walk($allCount, function ($v) use (&$counts) {
            if (key_exists($v['rights_status'], $counts)) {
                $counts[$v['rights_status']] = $v['counts'];
            }
        });
        return $counts;
    }
}